﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Irony.Ast;
using Irony.Interpreter;
using Irony.Parsing;

namespace ConciseMarkupLanguage.Ast
{
    class TermSimpleNode : AstNode
    {
        private AstNode _valueNode = null;
        private string _text = null;

        public override void Init(ParsingContext context, ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);
            _valueNode = (AstNode)treeNode.ChildNodes[0].AstNode;

            if (treeNode.ChildNodes[0].ChildNodes.Count > 0)
                AddChild("value", treeNode.ChildNodes[0]);
            else
            {
                AsString = treeNode.ChildNodes[0].FindTokenAndGetText();
                _text = treeNode.FindTokenAndGetText();
            }
        }

        public override void EvaluateNode(EvaluationContext context, AstMode mode)
        {
            switch (_text)
            {
                case "false":
                    context.Data.Push(false);
                    break;
                case "true":
                    context.Data.Push(true);
                    break;
                case "null":
                    context.Data.Push(null);
                    break;
                default:
                    _valueNode.Evaluate(context, AstMode.Read);
                    break;
            }
        }
    }
}
